package com.mall.thirdparth.pay.config;

import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.core.util.IOUtil;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @ClassName WxPayAutoCertificateConfig
 * @Author ymx
 * @Description 微信支付证书自动更新配置
 * @Date 2023/12/21
 **/
@Configuration
public class WxPayAutoCertificateConfig {

    // 微信支付配置
    @Resource
    private WxPayConfig wxPayConfig;

    /**
     * 初始化商户配置
     * @return
     */
    @Bean
    public RSAAutoCertificateConfig rsaAutoCertificateConfig() throws IOException {
        // 证书密钥
        String privateKey = IOUtil.toString(new ClassPathResource(wxPayConfig.getPrivateKey()).getInputStream());
        System.out.println("============"+privateKey);
        RSAAutoCertificateConfig config = new RSAAutoCertificateConfig.Builder()
            .merchantId(wxPayConfig.getMerchantId())
            .privateKey(privateKey)
            .merchantSerialNumber(wxPayConfig.getMerchantSerialNumber())
            .apiV3Key(wxPayConfig.getApiV3Key())
            .build();
        return config;
    }

}
